<#if noContractId??>
<h1>无效的合同信息</h1>
<#elseif notFound??>
<h1>未找到收款信息</h1>
<#else>
<form class="pay-form">
	<input type="hidden" name="id" value="${(data.id)!''}">
	<input type="hidden" name="contract.id" value="${(contractId)!''}">
	<div class="row">
		<div class="col-md-12 col-sm-12 col-xs-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div style="width: 88px;">
							<span class="text-danger">*</span>
							<span class="title-content">收款金额</span>
						</div>
					</div>
					<input type="text" class="form-control not-null" name="amount" placeholder="请输入数字" value="${data.amount}">
				</div>
			</div>
		</div>
		<div class="col-md-12 col-sm-12 col-xs-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div style="width: 88px;">
							<span class="text-danger">*</span>
							<span class="title-content">收款日期</span>
						</div>
					</div>
					<input type="text" class="form-control not-null datetimepicker" name="date" data-foot="false" data-format="yyyy-MM-dd" data-time="${data.date}" placeholder="请选择日期">
				</div>
			</div>
		</div>
		<div class="col-md-12 col-sm-12 col-xs-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div style="width: 88px;">
							<span class="title-content">备注</span>
						</div>
					</div>
					<textarea rows="5" class="form-control" name="des" placeholder="最多150个汉字">${data.des}</textarea>
				</div>
			</div>
		</div>
		<div class="col-md-12 col-sm-12 col-xs-12">
			<div class="btn btn-danger pull-right cancel" style="margin-left: 10px;">取消</div>
			<#if !data.id??>
			<div class="btn btn-warning pull-right continue" style="margin-left: 10px;">确定并继续添加</div>
			</#if>
			<div class="btn btn-primary pull-right ok">确定</div>
		</div>
	</div>
</form>
<script type="text/javascript">
	$(function() {
		$.suredy.datetimepicker.init();

		var validate = function($form) {
			var valid = true;
			$('.not-null', $form).each(function(i) {
				var $this = $(this);

				if ($this.val() === '') {
					valid = false;
					return false;
				}
			});

			return valid;
		};

		var edit = function(comeOn) {
			var $form = $('form.pay-form');
			var cId = $('input[name="contract.id"]', $form).val();

			if (!validate($form)) {
				alert('带有[*]项为必填项！请认真填写！');
				return false;
			}

			$form.ajaxSubmit({
				url : $.suredy.contextPath + '/payment/form/update',
				type : 'post',
				success : function(success, textStatus, jqXHR) {
					if (!success) {
						alert('新建/修改[收款信息]失败！');
						return false;
					}

					var listUri = $.suredy.contextPath + '/payment/list/' + cId;

					$.suredy.loadContent(listUri, '.pay-info', function() {
						// 重绘列表
						$.suredy.init_pay_list();

						var id = $('form.pay-form input[name=id]').val();
						if (id) {
							$('table.pay-list tr').each(function(i) {
								var $tr = $(this);

								if (id == $tr.data('id')) {
									$('.row-checkbox', $tr).trigger('click');
									return false;
								}
							});
						}
					});

					if (!comeOn) {
						$.suredy.closeModal();
					} else {
						$form.clearForm();
					}
				},
				error : function(jqXHR, textStatus, errorThrown) {
					alert('新建/修改[收款信息]失败！\n' + errorThrown);
				}
			});
		}

		$('.btn.ok').on('click', function(event) {
			edit(false);
		});

		$('.btn.continue').on('click', function(event) {
			edit(true);
		});

		$('.btn.cancel').on('click', function(event) {
			$.suredy.closeModal();
		});
	});
</script>
</#if>
